package ${packages.serviceImpl};

import ${packages.dto}.${classInfo.className}CreateRequest;
import ${packages.dto}.${classInfo.className}Response;
import ${packages.dto}.${classInfo.className}UpdateRequest;
import ${packages.dto}.${classInfo.className}QueryRequest;
import ${packages.entity}.${classInfo.className};
import ${packages.mapper}.${classInfo.className}Mapper;
import ${packages.service}.${classInfo.className}Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ${basePackage}.common.ErrorCode;
import ${basePackage}.common.ThrowUtils;
import ${basePackage}.common.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ${moduleDisplayName} 服务实现
 * ${moduleSummary}
<#if options.author??>
 * 作者：${options.author}
</#if>
 */
@Service
public class ${classInfo.className}ServiceImpl extends ServiceImpl<${classInfo.className}Mapper, ${classInfo.className}> implements ${classInfo.className}Service {

    @Override
    public ${classInfo.className}Response create(${classInfo.className}CreateRequest request) {
        ${classInfo.className} entity = new ${classInfo.className}();
        BeanUtils.copyProperties(request, entity);
        boolean saved = save(entity);
        ThrowUtils.throwIf(!saved, ErrorCode.OPERATION_ERROR, "保存${moduleDisplayName}失败");
        return toResponse(entity);
    }

    @Override
    public ${classInfo.className}Response update(${primaryKeyFieldType} ${primaryKeyFieldName}, ${classInfo.className}UpdateRequest request) {
        ${classInfo.className} entity = getById(${primaryKeyFieldName});
        ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR, "${moduleDisplayName}不存在");
        BeanUtils.copyProperties(request, entity);
        boolean updated = updateById(entity);
        ThrowUtils.throwIf(!updated, ErrorCode.OPERATION_ERROR, "更新${moduleDisplayName}失败");
        return toResponse(entity);
    }

    @Override
    public void delete(${primaryKeyFieldType} ${primaryKeyFieldName}) {
        boolean removed = removeById(${primaryKeyFieldName});
        ThrowUtils.throwIf(!removed, ErrorCode.NOT_FOUND_ERROR, "${moduleDisplayName}不存在");
    }

    @Override
    public ${classInfo.className}Response findById(${primaryKeyFieldType} ${primaryKeyFieldName}) {
        ${classInfo.className} entity = getById(${primaryKeyFieldName});
        ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR, "${moduleDisplayName}不存在");
        return toResponse(entity);
    }

    @Override
    public PageResponse<${classInfo.className}Response> page(${classInfo.className}QueryRequest request) {
        int pageNo = request.getPage() != null && request.getPage() > 0 ? request.getPage() : 1;
        int pageSize = request.getSize() != null && request.getSize() > 0 ? request.getSize() : 10;

        Page<${classInfo.className}> mpPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<${classInfo.className}> wrapper = Wrappers.lambdaQuery();

<#list classInfo.fieldList as field>
<#assign getter = field.fieldName?cap_first>
<#if field.fieldClass?contains("String")>
        if (request.get${getter}() != null && !request.get${getter}().isBlank()) {
            wrapper.eq(${classInfo.className}::get${getter}, request.get${getter}());
        }
<#else>
        if (request.get${getter}() != null) {
            wrapper.eq(${classInfo.className}::get${getter}, request.get${getter}());
        }
</#if>
</#list>

        page(mpPage, wrapper);
        List<${classInfo.className}Response> records = mpPage.getRecords().stream()
                .map(this::toResponse)
                .collect(Collectors.toList());
        return PageResponse.of(records, mpPage.getTotal(), pageNo, pageSize);
    }

    private ${classInfo.className}Response toResponse(${classInfo.className} entity) {
        if (entity == null) {
            return null;
        }
        ${classInfo.className}Response response = new ${classInfo.className}Response();
        BeanUtils.copyProperties(entity, response);
        return response;
    }
}
